package com.lagou.service.email.service.impl;

import com.lagou.common.util.CommonUtils;
import com.lagou.service.email.helper.MailHelper;
import com.lagou.service.email.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
public class EmailServiceImpl implements EmailService {

    @Value("${spring.mail.username}")
    private String mailAccount;

    @Value("${custom.code.subject}")
    private String subject;

    @Value("${custom.code.message-template}")
    private String messageTemplate;

    @Autowired
    private MailHelper mailHelper;

    @Override
    public Boolean send(String email, String code) {

        /** 1 取得消息模板，并填充内容 */
        String message = CommonUtils.messageFormat(messageTemplate, code);

        /** 2 发送邮件 */
        try {
            mailHelper.sendSimpleMail(mailAccount, email, subject, message);
        } catch (Exception e) {
            System.out.println("邮件发送失败");
            e.printStackTrace();
            // 发送失败的场合
            return false;
        }

        // 发送成功的场合
        return true;
    }
}
